package cn.itcast.controller;

import cn.itcast.domain.kefang;
import cn.itcast.domain.kehu;
import cn.itcast.service.impl.kefangService;
import cn.itcast.service.impl.kehuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;

@Controller
@RequestMapping(path = "/update")
public class UpdateController {
    @Autowired
    private kehuService kehuservice;
    @Autowired
    private kefangService kefangservice;

    @RequestMapping(path = "/kehu")
    public void kehu(kehu kh,HttpServletRequest request,HttpServletResponse response) throws IOException {
        kehuservice.updatekehu(kh);
        response.sendRedirect(request.getContextPath()+"/user/jump");
        return ;
    }
//    修改客户通过客房号，页面User_index专用
    @RequestMapping("/kehuByNum")
    public String kehuByNum(Integer num, Model model) throws IOException {
        kehu kh = kehuservice.findkehuByNum(num);

        model.addAttribute("num",kh.getNum());
        model.addAttribute("name",kh.getName());
        model.addAttribute("InTime",kh.getInTime());
        model.addAttribute("OutTime",kh.getOutTime());
        model.addAttribute("Id_num",kh.getId_num());

        return "kehu_update";
    }
    @RequestMapping("/kehu_null")
    public void kehu_null(Integer num,HttpServletRequest request,HttpServletResponse response) throws IOException {

        kehu kh = kehuservice.findkehuByNum(num);
        Date date = new Date(new java.util.Date().getTime());
        kh.setOutTime(date);

        kehuservice.updatekehu(kh);
        kefang kf = kefangservice.findkefangByNum(num);
        kf.setStatue(0);
        kefangservice.updatekefang(kf);
        response.sendRedirect(request.getContextPath()+"/user/jump");
        return ;
    }
    //在入住时更改客房状态,跳到填写客户信息的页面
    @RequestMapping(path = "/kefangstatue_In")
    public String kefangstatue_In(Integer num,Model model){
        model.addAttribute("num",num);
        return "FillIn_kehu";
    }

    //退房，返回首页
    @RequestMapping(path = "/kehuOut")
    public void kehuOut(String Id_num,HttpServletRequest request, HttpServletResponse response) throws IOException {
        kehu kh = kehuservice.findkehuById(Id_num);
        Date date = new Date(new java.util.Date().getTime());
        kh.setOutTime(date);
        kehuservice.updatekehu(kh);

        kefang kf = kefangservice.findkefangByNum(kh.getNum());
        kf.setStatue(0);
        kefangservice.updatekefang(kf);

        response.sendRedirect(request.getContextPath()+"/user/jump");
    }
}
